<?php
defined('BY_JYA') or exit('error');

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'课程列表'),
	array('op'=>'edit','title'=>'添加课程'),
);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['num']){
		$psize = $_RQ['num'];
	}else{
		$psize = 10;
	}
	$total='';
	$order='id desc';
	//print_r($params);exit;
	$list=pdo_getslice('jy_peiedu_kecheng',$params,array($pindex,$psize),$total,$fields,'',$order);
	if($list){
		foreach($list as &$item){
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'课程标题','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		)),
		'search_one'=>array('name'=>'title','help'=>'','add'=>false),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_peiedu_kecheng',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'课程',
		'tabs'=>$tabs,
		'edit'=>array(
			array('type'=>'string','title'=>'标题','name'=>'title', 'help'=>'' ),
			array('name'=>'status','title'=>'状态','type'=>'switch'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'status'=>intval($postdata['status']),
		'title'=>trim($postdata['title']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_peiedu_kecheng',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('修改成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_peiedu_kecheng',$data);
		exi('增加成功！','','list');
	}
}

if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择课程！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['paixu'])){
		$update['paixu']=$post['paixu'];
	}
	if(isset($post['status'])){
		$update['status']=$post['status'];
	}
	if($update){
		pdo_update('jy_peiedu_kecheng',$update,array('id'=>$id));
	}
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_peiedu_kecheng',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_peiedu_kecheng',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}